/*

Copyright (C) 2010  Christoph Schnack 

    This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package Wordcheck;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.TitledBorder;
import GuiTools.*;
import Mainframe.*;
import Statistics.*;
import Statistics.Statistics.WordStatistic;
import Storage.*;
import Settings.*;
import WordTable.WordTable;
import WordTable.WCSettings;

/**
 * 
 * Select vocabulary's words and options for word check.
 * 
 * @author Christoph Schnack
 * @see Wordcheck
 *
 */
	
public class WordcheckPreSelection extends TribuitWordChooser{
	PreOptionPanel OPTIONPANEL;
	GroupsPanel groupsPanel;
	Vector<Word> statVocabulary;;
	TribuitMenu menu;
	Statistics STATISTICS;
	boolean LEARNFIRST=false,CHOSERANDOMWORDS;
	int RANDOMNUMBER;
	HashMap<String,String> lastSetup = new HashMap<String,String>();
	TribuitWordChooser wordChooser;
	JRadioButton chooseAllWordsRB,chooseRandomWordsRB;
	JSpinner randomWordsCounter;
	HashMap<String,TermClass> termClassesA,termClassesB,statTermClassesA,statTermClassesB;
	
	public WordcheckPreSelection(Storage mainvoc, Settings set, Mainframe mf,TribuitMenu m){
		super(mainvoc,set,mf,set.getLanguage().getMenuWordcheck(),set.getLanguage().getDisplayed());
		wordChooser=this;
		menu=m;
		STATISTICS = MAINVOC.getStatistics();
		buildGUI();
		preTestFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				menu.setWordcheckRunning(false);
			}
		});
		preTestOkBtn.setText(LANG.getStart());
		lastSetup = set.getWordCheckPresettings();
		if(lastSetup!=null){
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					setLastSetup();	
				}
			});
		}
	}
	
	public JPanel getWordsPanel(){
		wordsLabel = new JLabel("0");
		JLabel wordsLabelLabel = new JLabel(LANG.getWords()+": ");
		JPanel wordsPanel = new JPanel();
		wordsPanel.add(wordsLabelLabel);
		wordsPanel.add(wordsLabel);
		SpinnerModel model = new SpinnerNumberModel(25,1,1000,1); 
		randomWordsCounter = new JSpinner(model);
		randomWordsCounter.setEnabled(false);
		randomWordsCounter.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				RANDOMNUMBER=(Integer)randomWordsCounter.getValue();
				generateTestVocabulary();
				preTestFrame.pack();
			}
		});
		chooseRandomWordsRB = new JRadioButton(LANG.getChoseRandomWordsCB(),false);
		chooseRandomWordsRB.setToolTipText(LANG.getChoseRandomWordsCBTTT());
		chooseRandomWordsRB.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					CHOSERANDOMWORDS = true;
					randomWordsCounter.setEnabled(true);
					RANDOMNUMBER=(Integer)randomWordsCounter.getValue();
					generateTestVocabulary();
					preTestFrame.pack();
				}
			}
		);
		chooseAllWordsRB = new JRadioButton(LANG.getAll(),true);
		chooseAllWordsRB.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						CHOSERANDOMWORDS = false;
						randomWordsCounter.setEnabled(false);
						generateTestVocabulary();
						preTestFrame.pack();
					}
				}
			);
		ButtonGroup bg = new ButtonGroup();
		bg.add(chooseAllWordsRB);
		bg.add(chooseRandomWordsRB);
		JPanel wordsRndPanel = new JPanel();
		wordsRndPanel.add(chooseAllWordsRB);
		wordsRndPanel.add(chooseRandomWordsRB);
		wordsRndPanel.add(randomWordsCounter);
		JPanel returnPanel = new JPanel();
		returnPanel.add(wordsPanel);
		returnPanel.add(wordsRndPanel);
		return returnPanel;
	}
	
	public JPanel groupsPanel(){
		groupsPanel = new GroupsPanel();
		return groupsPanel;
	}
	
	public JPanel optionPanel(){
		OPTIONPANEL = new PreOptionPanel();
		return OPTIONPANEL;
	}
	
	public void updatePreCB(){
		Vocabulary vocTmp = MAINVOC.getVocabulary((String)preCB.getSelectedItem());
		lang1RB.setText(vocTmp.getNameA());
		lang2RB.setText(vocTmp.getNameB());
		updateTermClassCB();
		updateSections();
		allSectionsBtn.setText(LANG.getAll());
		HashMap<String,WordStatistic> stats = STATISTICS.getVocabularyStatistics(vocTmp.getName(),true);
		long oldest = Long.MAX_VALUE,last;
		if(!stats.isEmpty()){
			for(String s : stats.keySet()){
				last = stats.get(s).getLastCheckedDate();
				if((last>0)&&(last<oldest)) oldest=last;
			}
			Date d = new Date();
			if(OPTIONPANEL!=null){
				if(oldest!=Long.MAX_VALUE)groupsPanel.setDaySpinnerValue((int)((d.getTime()-oldest)/(24*60*60000)));
				else groupsPanel.setDaySpinnerValue(1);
			}
		}
		preTestFrame.pack();
		preTestFrame.repaint();
	}
	
	public void updateSections() {
		// find sections containing words of the selected term class
		Vector<String> selectedTermClasses = new Vector<String>();
		for (JCheckBox tcJCB : tcCB) {
			if (tcJCB.isSelected())selectedTermClasses.add(tcJCB.getText());
		}
		String tc, section,vocName = preCB.getSelectedItem().toString();
		Word[] wordsTmp = MAINVOC.getVocabulary(vocName).getWords();
		availableSections = new Vector<String>();
		if(groupsPanel.isStatOptions()){
			HashMap<String,WordStatistic> vocStats = STATISTICS.getVocabularyStatistics(vocName,true);
			WordStatistic ws;
			for (Word w : wordsTmp) {
				tc = w.getTermclassA() + " / " + w.getTermclassB();
				ws = vocStats.get(w.getName());
				if (selectedTermClasses.contains(tc) && groupsPanel.checkWord(vocName, w, ws)) {
					section = w.getLection();
					if (!availableSections.contains(section))availableSections.add(section);
				}
			}
		}
		else{
			for (Word w : wordsTmp) {
				tc = w.getTermclassA() + " / " + w.getTermclassB();
				if (selectedTermClasses.contains(tc)) {
					section = w.getLection();
					if (!availableSections.contains(section))
						availableSections.add(section);
				}
			}
		}
		// sorting the selected sections
		ArrayList<String> sectionWords = new ArrayList<String>();
		ArrayList<Integer> sectionNumbers = new ArrayList<Integer>();
		Integer in;
		// split numbers from words
		for (int i = 0; i < availableSections.size(); i++) {
			section = availableSections.elementAt(i);
			try {
				in = Integer.parseInt(section);
				if (!sectionNumbers.contains(in))sectionNumbers.add(in);
			}
			catch (Exception e) {
				if (!sectionWords.contains(section))sectionWords.add(section);
			}
		}
		availableSections.removeAllElements();
		if (sectionNumbers.size() > 0) {
			Collections.sort(sectionNumbers);
			for (int number : sectionNumbers) {
				availableSections.add("" + number);
			}
		}
		if (sectionWords.size() > 0) {
			Collections.sort(sectionWords, String.CASE_INSENSITIVE_ORDER);
			for (String word : sectionWords) {
				availableSections.add(word);
			}
		}
		lectionsPanel.removeAll();
		lectionsCB = new JCheckBox[availableSections.size()];
		String toAdd;
		for (int i = 0; i < availableSections.size(); i++) {
			toAdd = availableSections.elementAt(i).toString();
			lectionsCB[i] = new JCheckBox((toAdd.equals("~") || toAdd.equals("~~~")) ? LANG.getUnsorted() : toAdd, false);
			lectionsCB[i].setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
			lectionsPanel.add(lectionsCB[i]);
			lectionsCB[i].addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					for(JCheckBox cb : lectionsCB){
						if(cb.isSelected()){
							allSectionsBtn.setText(LANG.getNone());
							preTestFrame.pack();
							return;
						}
					}
					allSectionsBtn.setText(LANG.getAll());
				}
			});
			lectionsCB[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					generateTestVocabulary();
				}
			});
		}
		generateTestVocabulary();
		preTestFrame.pack();
		preTestFrame.repaint();
	}
	
	public void okAction(String vocName,Vector<Word> words){
		WCSettings wcSettings = new WCSettings(words,vocNameA,vocNameB);
		wcSettings.setTestLang2Arg(this.getTestLanguage2());
		wcSettings.setTestMetasArg(true);
		wcSettings.setShowPhoneticsArg(true);
		wcSettings.setShowNoteArg(true);
		wcSettings.setShowSynonymsArg(true);
		if(LEARNFIRST)new WordTable(MAINVOC,SET,menu,MAINFRAME,wcSettings,OPTIONPANEL,termClassesA,termClassesB);
		else new Wordcheck(this.getTestLanguage2(),OPTIONPANEL, MAINVOC, SET, MAINFRAME,menu,words,vocNameA,vocNameB,termClassesA,MAINVOC.getVocabulary(vocName).getCategoriesA(),termClassesB,MAINVOC.getVocabulary(vocName).getCategoriesB());
		saveSetup();
	}
	
	public void cancelAction(){menu.setWordcheckRunning(false);}
	
	public void generateTestVocabulary(){
		Vocabulary tmpVoc = MAINVOC.getVocabulary((String)preCB.getSelectedItem());
		testvocabulary = new Vector<Word>();
		vocNameA = tmpVoc.getNameA();
		vocNameB = tmpVoc.getNameB();
		termClassesA = tmpVoc.tcsA;
		termClassesB = tmpVoc.tcsB;
		Word[] tmp_wordlist = tmpVoc.getWords();
		Vector<String> tcSelection = new Vector<String>();
		Vector<String> sectionSelection = new Vector<String>();
		for(JCheckBox tcJCB : tcCB){
			if(tcJCB.isSelected())tcSelection.add(tcJCB.getText());
		}
		for(int i=0;i<lectionsCB.length;i++){
			if(lectionsCB[i].isSelected()) sectionSelection.add(lectionsCB[i].getText());
		}
		if(tcSelection.size()>0 && sectionSelection.size()>0){
			Vector<Word> wordsToTest = new Vector<Word>();
			if(groupsPanel.isStatOptions()){
				HashMap<String,WordStatistic> vocStats = STATISTICS.getVocabularyStatistics(tmpVoc.getName(),true);
				WordStatistic ws;
				for(int i=0;i<tmp_wordlist.length;i++){
					ws = vocStats.get(tmp_wordlist[i].getName());
					if(tcSelection.contains(tmp_wordlist[i].getTermclassA()+" / " + tmp_wordlist[i].getTermclassB()) && (sectionSelection.contains(tmp_wordlist[i].getLection())) && (groupsPanel.checkWord(tmpVoc.getName(), tmp_wordlist[i],ws))) wordsToTest.add(tmp_wordlist[i]);
				}
			}
			else{
				for(int i=0;i<tmp_wordlist.length;i++){
					if(tcSelection.contains(tmp_wordlist[i].getTermclassA()+" / " + tmp_wordlist[i].getTermclassB()) && (sectionSelection.contains(tmp_wordlist[i].getLection()))) wordsToTest.add(tmp_wordlist[i]);
				}
			}
			Collections.shuffle(wordsToTest);
			if(CHOSERANDOMWORDS && RANDOMNUMBER<wordsToTest.size()){
				for(int i=0;i<RANDOMNUMBER;i++){
					testvocabulary.add(wordsToTest.elementAt(i));
				}
			}
			else testvocabulary = wordsToTest;
			wordsLabel.setText(""+wordsToTest.size());
		}
		else{
			wordsLabel.setText("0");
		}
	}
	
	
	private void saveSetup(){
		SET.setWordCheckPresettings(
			"vocabulary:"+(String)preCB.getSelectedItem()+","+
			"firstLanguage:"+(lang1RB.isSelected()?"1":"2")+","+
			"learnFirstCB:"+(LEARNFIRST?"true":"false")+","+
			"testMetasCB:"+(OPTIONPANEL.isTestmetas()?"true":"false")+","+
			"testSynonymsCB:"+(OPTIONPANEL.isTestSynonyms()?"true":"false")+","+
			"testmodusCB:"+(OPTIONPANEL.isTestmodus()?"true":"false")+","+
			"testAllMeaningsCB:"+(OPTIONPANEL.isTestallmeanings()?"true":"false")+","+
			"writeStatsCB:"+(OPTIONPANEL.isWriteStats()?"true":"false")+","+
			"statOptionsCB:"+(groupsPanel.isStatOptions()?"true":"false")+","+
			"oftenWrongCB:"+(groupsPanel.isOftenWrong()?"true":"false")+","+
			"neverTestedCB:"+(groupsPanel.isNeverTested()?"true":"false")+","+
			"notTestedSinceCB:"+(groupsPanel.isNotTestedSince()?"true":"false")+","+
			"percentSpinner:"+(groupsPanel.getPercentSpinner())+","+
			"allWords:"+(chooseAllWordsRB.isSelected()?"true":"false")+","+
			"randomNumber:"+RANDOMNUMBER
		);
		SET.save();
	}
	
	private void setLastSetup(){
		preCB.setSelectedItem(lastSetup.get("vocabulary"));
		if(lastSetup.get("firstLanguage").equals("1")) lang1RB.doClick();
		else lang2RB.doClick();
		OPTIONPANEL.setLearnFirstCB(lastSetup.get("learnFirstCB").equals("true"));
		OPTIONPANEL.setTestmetasCB(lastSetup.get("testMetasCB").equals("true"));
		OPTIONPANEL.setTestmodusCB(lastSetup.get("testmodusCB").equals("true"));
		OPTIONPANEL.setTestSynonymsCB(lastSetup.get("testSynonymsCB").equals("true"));
		OPTIONPANEL.setTestAllMeaningsCB(lastSetup.get("testAllMeaningsCB").equals("true"));
		OPTIONPANEL.setWriteStatsCB(lastSetup.get("writeStatsCB").equals("true"));
		groupsPanel.setStatOptionsCB(lastSetup.get("statOptionsCB").equals("true"));
		if(lastSetup.get("statOptionsCB").equals("true")){
			groupsPanel.setNeverTestedCB(lastSetup.get("neverTestedCB").equals("true"));
			groupsPanel.setOftenWrongCB(lastSetup.get("oftenWrongCB").equals("true"));
			groupsPanel.setNotTestedSinceCB(lastSetup.get("notTestedSinceCB").equals("true"));
			groupsPanel.setPercentSpinner(Integer.parseInt(lastSetup.get("percentSpinner")));
		}
		if(!lastSetup.get("allWords").equals("true")){
			randomWordsCounter.setValue(Integer.parseInt(lastSetup.get("randomNumber")));
			chooseRandomWordsRB.doClick();
		}
		
	}
	
	private class GroupsPanel extends JPanel{
		JSpinner daySpinner, percentSpinner;
		JCheckBox neverTestedCB,notTestedSinceCB,oftenWrongCB,statOptionsCB;
		JRadioButton moreThanRB,lessThanRB,moreThanDaysRB,lessThanDaysRB;
		boolean moreThan=true,moreThanPercent=true;
		boolean notSince, never, oftenWrong;
		
		Calendar calendar = Calendar.getInstance();
		int days,percent=50,day = 24*60*60000;
		long today = calendar.getTimeInMillis(),daysGone = ((long)days)*day, date;
		public GroupsPanel(){
			super();
			statOptionsCB = new JCheckBox(LANG.getActivate(),false);
			statOptionsCB.addActionListener(new StatOptionsCBListener());
			SpinnerModel dayModel = new SpinnerNumberModel(1,1,1000,1);
			daySpinner = new JSpinner(dayModel);
			daySpinner.addChangeListener(new ChangeListener(){
				public void stateChanged(ChangeEvent arg0) {
					days = (Integer)daySpinner.getValue();
					daysGone = ((long)days)*day;
					updateSections();
				}
			});
			moreThanDaysRB = new JRadioButton(">",true);
			moreThanDaysRB.setToolTipText(LANG.getMoreThan());
			moreThanDaysRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					moreThan=true;
					updateSections();
				}
			});
			lessThanDaysRB = new JRadioButton("<");
			lessThanDaysRB.setToolTipText(LANG.getLessThan());
			lessThanDaysRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					moreThan=false;
					updateSections();
				}
			});
			ButtonGroup lbg = new ButtonGroup();
			lbg.add(moreThanDaysRB);
			lbg.add(lessThanDaysRB);
			JPanel moreDaysPanel = new JPanel();
			moreDaysPanel.add(moreThanDaysRB);
			moreDaysPanel.add(lessThanDaysRB);
			SpinnerModel percentModel = new SpinnerNumberModel(50,0,100,1);
			percentSpinner = new JSpinner(percentModel);
			percentSpinner.addChangeListener(new ChangeListener(){
				public void stateChanged(ChangeEvent arg0) {
					percent = (Integer)percentSpinner.getValue();
					updateSections();
				}
			});
			moreThanRB = new JRadioButton(">",true);
			moreThanRB.setToolTipText(LANG.getMoreThan());
			moreThanRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					moreThanPercent=true;
					updateSections();
				}
			});
			lessThanRB = new JRadioButton("<");
			lessThanRB.setToolTipText(LANG.getLessThan());
			lessThanRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					moreThanPercent=false;
					updateSections();
				}
			});
			ButtonGroup lbg2 = new ButtonGroup();
			lbg2.add(moreThanRB);
			lbg2.add(lessThanRB);
			JPanel morePanel = new JPanel();
			morePanel.add(moreThanRB);
			morePanel.add(lessThanRB);
			neverTestedCB = new JCheckBox(LANG.getNeverAsked(),false);
			neverTestedCB.addItemListener(new ItemListener(){
				public void itemStateChanged(ItemEvent e){
					never = neverTestedCB.isSelected();
				}
			});
			neverTestedCB.setOpaque(false);
			neverTestedCB.addActionListener(new StatCBListener());
			notTestedSinceCB = new JCheckBox(LANG.getNotAskedSince(),false);
			notTestedSinceCB.setOpaque(false);
			notTestedSinceCB.addActionListener(new StatCBListener());
			notTestedSinceCB.addItemListener(new ItemListener(){
				public void itemStateChanged(ItemEvent e){
					notSince = notTestedSinceCB.isSelected();
				}
			});
			oftenWrongCB = new JCheckBox(LANG.getAnsweredWrong(),false);
			oftenWrongCB.setOpaque(false);
			oftenWrongCB.addActionListener(new StatCBListener());
			oftenWrongCB.addItemListener(new ItemListener(){
				public void itemStateChanged(ItemEvent e){
					oftenWrong = oftenWrongCB.isSelected();
				}
			});

			JLabel daySpinnerLabel = new JLabel(LANG.getDays());
			daySpinnerLabel.setOpaque(false);
			
			JLabel percentSpinnerLabel = new JLabel("%");
			percentSpinnerLabel.setOpaque(false);
			
			boolean selected = false;
			notTestedSinceCB.setEnabled(selected);
			oftenWrongCB.setEnabled(selected);
			neverTestedCB.setEnabled(selected);
			daySpinner.setEnabled(selected);
			percentSpinner.setEnabled(selected);
			moreThanRB.setEnabled(selected);
			lessThanRB.setEnabled(selected);
			moreThanDaysRB.setEnabled(selected);
			lessThanDaysRB.setEnabled(selected);
			
			JPanel statOptionsPanel = new JPanel();
			statOptionsPanel.setOpaque(false);
			statOptionsPanel.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.WEST;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.weighty=1;
			gbc.insets= new Insets(10,0,10,0);
			statOptionsPanel.add(statOptionsCB,gbc);
			gbc.insets= new Insets(0,0,0,0);
			gbc.gridy=1;
			statOptionsPanel.add(neverTestedCB,gbc);
			gbc.gridy=2;
			statOptionsPanel.add(notTestedSinceCB,gbc);
			gbc.gridy=3;
			statOptionsPanel.add(oftenWrongCB,gbc);
			gbc.gridx=1;
			gbc.gridy=2;
			statOptionsPanel.add(moreDaysPanel,gbc);
			gbc.gridy=3;
			statOptionsPanel.add(morePanel,gbc);
			gbc.gridx=2;
			gbc.gridy=2;
			statOptionsPanel.add(daySpinner,gbc);
			gbc.gridy=3;
			statOptionsPanel.add(percentSpinner,gbc);
			gbc.gridx=3;
			gbc.gridy=2;
			statOptionsPanel.add(daySpinnerLabel,gbc);
			gbc.gridy=3;
			statOptionsPanel.add(percentSpinnerLabel,gbc);
			
			this.setLayout(new GridBagLayout());
			GridBagConstraints gpGBC=new GridBagConstraints();
			gpGBC.anchor=GridBagConstraints.NORTHWEST;
			gpGBC.insets=new Insets(5,5,5,5);
			this.add(new LabelPanel(LANG.getStatisticOptions()+":",statOptionsPanel),gpGBC);
			gpGBC.gridx=1;
			this.add(new LabelPanel(LANG.getTermclasses()+":",tcPanePanel),gpGBC);
			gpGBC.gridx=2;
			this.add(new LabelPanel(LANG.getLections()+":",lectionsPanePanel),gpGBC);
			this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black,3),LANG.getWordgroups()+":",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.DEFAULT_POSITION,new Font(Font.SANS_SERIF,Font.BOLD|Font.ITALIC,16)));
		}
		
		class StatOptionsCBListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				JCheckBox source = (JCheckBox) e.getSource();
				boolean selected = source.isSelected();
				notTestedSinceCB.setEnabled(selected);
				oftenWrongCB.setEnabled(selected);
				neverTestedCB.setEnabled(selected);
				daySpinner.setEnabled(selected);
				percentSpinner.setEnabled(selected);
				moreThanRB.setEnabled(selected);
				lessThanRB.setEnabled(selected);
				moreThanDaysRB.setEnabled(selected);
				lessThanDaysRB.setEnabled(selected);
				updateSections();
			}
		}
		
		class StatCBListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				updateSections();
			}
		}
		
		
		public boolean checkWord(String vocName,Word w,WordStatistic ws){
			if(statOptionsCB.isSelected() && (notSince || never || oftenWrong)){
				date = ws.getLastCheckedDate();
				if(notSince){
					if(moreThan){
						if((date>0 && (today-date)<daysGone)||(!never && date==0)) return false;
					}
					else if((date>0 && (today-date)>daysGone)||(!never && date==0)) return false;
				}
				if(never){
					if(!notSince && date!=0) return false;
				}
				if(oftenWrong){
					if(moreThanPercent){if(percent>ws.getPercentWrong()) return false;}
					else if(percent<ws.getPercentWrong()) return false;
				}
				return true;
			}
			else return true;
		}
		
		public boolean isStatOptions(){ return statOptionsCB.isSelected();}
		public boolean isOftenWrong(){ return oftenWrongCB.isSelected();}
		public boolean isNeverTested(){ return neverTestedCB.isSelected();}
		public boolean isNotTestedSince(){ return notTestedSinceCB.isSelected();}
		public int getPercentSpinner(){ return (Integer)percentSpinner.getValue();}
		
		public void setStatOptionsCB(boolean bool){
			if((bool && !statOptionsCB.isSelected()) || (!bool && statOptionsCB.isSelected())) statOptionsCB.doClick();
		}
		public void setOftenWrongCB(boolean bool){
			if((bool && !oftenWrongCB.isSelected()) || (!bool && oftenWrongCB.isSelected())) oftenWrongCB.doClick();
		}
		public void setNeverTestedCB(boolean bool){
			if((bool && !neverTestedCB.isSelected()) || (!bool && neverTestedCB.isSelected())) neverTestedCB.doClick();
		}
		public void setNotTestedSinceCB(boolean bool){
			if((bool && !notTestedSinceCB.isSelected()) || (!bool && notTestedSinceCB.isSelected())) notTestedSinceCB.doClick();
		}
		public void setPercentSpinner(int arg){
			percentSpinner.setValue(arg);
			percent = (Integer)percentSpinner.getValue();
		}
		
		public void setDaySpinnerValue(int arg){
			days = arg;
			daySpinner.setValue(arg);
		}
	}
	
	public class PreOptionPanel extends JPanel{
		boolean TESTMETAS=true,TESTMODUS,TESTALLMEANINGS=true,WRITESTATS=false,TESTSYNONYMS=true;
		JCheckBox writeStatsCB,testmetasCB,testmodusCB,testAllMeaningsCB,learnFirstCB,testSynonymsCB;
		
		public PreOptionPanel(){
			//select if metas are tested
			testmetasCB = new JCheckBox(LANG.getMetainformation(),true);
			testmetasCB.setToolTipText(LANG.getTestmetasCBTTText());
			testmetasCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					TESTMETAS = testmetasCB.isSelected();
				}
			});
			//select if wrong answered words are asked again and result is given
			testmodusCB = new JCheckBox(LANG.getTestMode());
			testmodusCB.setToolTipText(LANG.getTestmodusCBText());
			testmodusCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					TESTMODUS = testmodusCB.isSelected();
				}
			});
			
			writeStatsCB = new JCheckBox(LANG.getAffectStatistic());
			writeStatsCB.setToolTipText(LANG.getCollectStatisticsTTT());
			writeStatsCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					WRITESTATS = writeStatsCB.isSelected();
				}
			});
			writeStatsCB.doClick();
			testAllMeaningsCB = new JCheckBox(LANG.getAllMeanings(),true);
			testAllMeaningsCB.setToolTipText(LANG.getTestAllMeaningsTTT());
			testAllMeaningsCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					TESTALLMEANINGS = testAllMeaningsCB.isSelected();
				}
			});
			learnFirstCB = new JCheckBox(LANG.getLearnFirst(),false);
			learnFirstCB.setToolTipText(LANG.getLearnFirstTTT());
			learnFirstCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					LEARNFIRST = learnFirstCB.isSelected();
				}
			});
			
			testSynonymsCB = new JCheckBox(LANG.getSynonyms(),true);
			testSynonymsCB.setToolTipText(LANG.getTestSynonymsTTT());
			testSynonymsCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					TESTSYNONYMS = testSynonymsCB.isSelected();
				}
			});
			
			JPanel cbPanel = new JPanel();
			cbPanel.setLayout(new BoxLayout(cbPanel,BoxLayout.Y_AXIS));
			cbPanel.add(testmetasCB);
			cbPanel.add(testmodusCB);
			cbPanel.add(writeStatsCB);
			JPanel cbPanel2 = new JPanel();
			cbPanel2.setLayout(new BoxLayout(cbPanel2,BoxLayout.Y_AXIS));
			cbPanel2.add(testAllMeaningsCB);
			cbPanel2.add(testSynonymsCB);
			cbPanel2.add(learnFirstCB);
			JPanel crwPanel = new JPanel();
			GridBagConstraints crwGBC = new GridBagConstraints(); 
			crwPanel.setLayout(new GridBagLayout());
			crwGBC.fill = GridBagConstraints.VERTICAL;
			crwGBC.anchor = GridBagConstraints.NORTHWEST;
			crwPanel.add(cbPanel2,crwGBC);
			
			
			this.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.insets = new Insets(5,5,5,5);
			gbc.anchor = GridBagConstraints.NORTHWEST;
			gbc.fill = GridBagConstraints.NONE;
			this.add(cbPanel,gbc);
			gbc.gridx=1;
			this.add(crwPanel,gbc);
		}
		
		public void setTestAllMeaningsCB(boolean bool){
			if(bool && !testAllMeaningsCB.isSelected())testAllMeaningsCB.doClick();
			if(!bool && testAllMeaningsCB.isSelected()) testAllMeaningsCB.doClick();
		}
		
		public void setLearnFirstCB(boolean bool){
			if(bool && !learnFirstCB.isSelected())learnFirstCB.doClick();
			if(!bool && learnFirstCB.isSelected()) learnFirstCB.doClick();
		}
		
		public void setTestmodusCB(boolean bool){
			if(bool && !testmodusCB.isSelected())testmodusCB.doClick();
			if(!bool && testmodusCB.isSelected()) testmodusCB.doClick();
		}
		
		public void setWriteStatsCB(boolean bool){
			if(bool && !writeStatsCB.isSelected())writeStatsCB.doClick();
			if(!bool && writeStatsCB.isSelected()) writeStatsCB.doClick();
		}
		
		public void setTestmetasCB(boolean bool){
			if(bool && !testmetasCB.isSelected())testmetasCB.doClick();
			if(!bool && testmetasCB.isSelected()) testmetasCB.doClick();
		}
		
		public void setTestSynonymsCB(boolean bool){
			if(bool && !testSynonymsCB.isSelected())testSynonymsCB.doClick();
			if(!bool && testSynonymsCB.isSelected()) testSynonymsCB.doClick();
		}
		
		public boolean isTestmodus(){ return TESTMODUS;}
		public boolean isTestmetas(){ return TESTMETAS;}
		public boolean isTestallmeanings(){ return TESTALLMEANINGS;}
		public boolean isTestSynonyms(){ return TESTSYNONYMS;}
		public boolean isWriteStats(){ return WRITESTATS;}

	}
}
